﻿using System;
using System.Reflection;
using UtilZ.Dotnet.Ex.Base;

namespace UtilZ.Dotnet.Ex.Json
{
    /// <summary>
    /// 整数十六进制 json转换器
    /// </summary>
    public class HexJsonValueConverter : JsonValueConverterAbs
    {
        private readonly static HexJsonValueConverter _default;
        /// <summary>
        /// 默认实例
        /// </summary>
        public static HexJsonValueConverter Default
        {
            get { return _default; }
        }

        static HexJsonValueConverter()
        {
            _default = new HexJsonValueConverter();
        }





        /// <summary>
        /// 构造函数
        /// </summary>
        public HexJsonValueConverter()
            : base(false)
        {

        }





        /// <summary>
        /// 尝试将对象值转换为json值[转换成功返回true,失败返回false]
        /// </summary>
        /// <param name="memberInfo">成员信息</param>
        /// <param name="valueType">值类型</param>
        /// <param name="owner">成员所属对象</param>
        /// <param name="value">属性或字段值</param>
        /// <param name="jsonValue">成员值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertTo(MemberInfo memberInfo, Type valueType, object owner, object value, out string jsonValue)
        {
            if (value == null)
            {
                jsonValue = null;
            }
            else
            {
                switch (Type.GetTypeCode(valueType))
                {
                    case TypeCode.Char:
                        jsonValue = ConvertEx.ToHexadecimaString((char)value);
                        break;
                    case TypeCode.SByte:
                        jsonValue = ConvertEx.ToHexadecimaString((sbyte)value);
                        break;
                    case TypeCode.Byte:
                        jsonValue = ConvertEx.ToHexadecimaString((byte)value);
                        break;
                    case TypeCode.Int16:
                        jsonValue = ConvertEx.ToHexadecimaString((short)value);
                        break;
                    case TypeCode.UInt16:
                        jsonValue = ConvertEx.ToHexadecimaString((ushort)value);
                        break;
                    case TypeCode.Int32:
                        jsonValue = ConvertEx.ToHexadecimaString((int)value);
                        break;
                    case TypeCode.UInt32:
                        jsonValue = ConvertEx.ToHexadecimaString((uint)value);
                        break;
                    case TypeCode.Int64:
                        jsonValue = ConvertEx.ToHexadecimaString((long)value);
                        break;
                    case TypeCode.UInt64:
                        jsonValue = ConvertEx.ToHexadecimaString((ulong)value);
                        break;
                    case TypeCode.Single:
                        jsonValue = ConvertEx.ToHexadecimaString((float)value);
                        break;
                    case TypeCode.Double:
                        jsonValue = ConvertEx.ToHexadecimaString((double)value);
                        break;
                    case TypeCode.Decimal:
                    default:
                        throw new NotSupportedException(valueType.ToString());
                }
            }

            return true;
        }





        /// <summary>
        /// 尝试将json值转换为对象值[转换成功返回true,失败返回false]
        /// </summary>
        /// <param name="memberInfo">成员信息</param>
        /// <param name="valueType">值类型</param>
        /// <param name="owner">成员所属对象</param>
        /// <param name="jsonValue">json值</param>
        /// <param name="value">对象值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        protected override bool PrimitiveTryConvertFrom(MemberInfo memberInfo, Type valueType, object owner, string jsonValue, out object value)
        {
            throw new NotImplementedException("有空再来实现，居然没有写从十六进制转换回来的方法.");
            //bool result = true;

            //if (string.IsNullOrEmpty(jsonValue) || JsonHelper.IsNull(jsonValue))
            //{
            //    value = null;
            //}
            //else
            //{
            //    switch (Type.GetTypeCode(valueType))
            //    {
            //        case TypeCode.Char:
            //            jsonValue = ConvertEx.ConvertTo((char)value,1);
            //            break;
            //        case TypeCode.SByte:
            //            jsonValue = ConvertEx.ToHexadecimaString((sbyte)value);
            //            break;
            //        case TypeCode.Byte:
            //            jsonValue = ConvertEx.ToHexadecimaString((byte)value);
            //            break;
            //        case TypeCode.Int16:
            //            jsonValue = ConvertEx.ToHexadecimaString((short)value);
            //            break;
            //        case TypeCode.UInt16:
            //            jsonValue = ConvertEx.ToHexadecimaString((ushort)value);
            //            break;
            //        case TypeCode.Int32:
            //            jsonValue = ConvertEx.ToHexadecimaString((int)value);
            //            break;
            //        case TypeCode.UInt32:
            //            jsonValue = ConvertEx.ToHexadecimaString((uint)value);
            //            break;
            //        case TypeCode.Int64:
            //            jsonValue = ConvertEx.ToHexadecimaString((long)value);
            //            break;
            //        case TypeCode.UInt64:
            //            jsonValue = ConvertEx.ToHexadecimaString((ulong)value);
            //            break;
            //        case TypeCode.Single:
            //            jsonValue = ConvertEx.ToHexadecimaString((float)value);
            //            break;
            //        case TypeCode.Double:
            //            jsonValue = ConvertEx.ToHexadecimaString((double)value);
            //            break;
            //        case TypeCode.Decimal:
            //        default:
            //            throw new NotSupportedException(valueType.ToString());
            //    }
            //    value = jsonValue;
            //}

            //return result;
        }
    }
}
